#!/usr/bin/python

import flickrapi
import urllib2
import sys

address = '/home/gromain/DPF/'
picnum = int(sys.argv[1])
picmaxFile = open(address + 'flickr/picstate', 'r')
picmax = int(picmaxFile.read())
picmaxFile.close()

api_key = '8e2f246a90c71a91d0e96bcd99659e55'

flickr = flickrapi.FlickrAPI(api_key)

print 'Flickr, picture list download...'

photos = flickr.interestingness_getList(per_page = picnum)

print 'Flickr picture download started...'

for i in range(0, picnum):
    photo = photos.__getitem__(0)[i]
    farm = photo.get('farm')
    server = photo.get('server')
    secret = photo.get('secret')
    picid = photo.get('id')
    url = 'http://farm' + farm + '.static.flickr.com/' + server + '/' + picid + '_'+ secret + '_b.jpg'
    picname = 'pic' + str(picmax + i + 1) + '.jpg'
    u = urllib2.urlopen(url)
    localFile = open('/home/gromain/Pictures/' + picname, 'w')
    localFile.write(u.read())
    localFile.close()
    print picname + ' => Done, ' + str(picnum-i-1) + ' left...'


picmax = picmax + picnum
picmaxFile = open(address + 'flickr/picstate', 'w')
picmaxFile.write(str(picmax))
picmaxFile.close()
print 'Number of pictures: ' + str(picmax)
print 'Flickr, Done!'
